package ljye.com.ljye.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import ljye.com.ljye.R;
import ljye.com.ljye.activity.Activity_map;
import ljye.com.ljye.adapter.Introduce_IMG_adapter;
import ljye.com.ljye.utils.CommonViewHolder;


/**
 * 机构主页
 */
public class Fragment_Institution_home_resume extends Fragment {

    private View view;
    private ListView listview;
    private ListView listviewImg;
    private ArrayList<String> listData;
    private ClassAdpapter mLvAdapter;
    private TextView classAddress;
    private LinearLayout address;
    

    public View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.address:
                startActivity(new Intent(getActivity(), Activity_map.class));
                    break;
                default:
                    break;
            }
        }
    };
    private List<String> imglist;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view) {
            view = inflater.inflate(R.layout.fragment_institution_home_resume, container, false);
            listview = (ListView)view.findViewById(R.id.listview);
            listviewImg = (ListView)view.findViewById(R.id.listview_img);
            classAddress = (TextView)view.findViewById(R.id.class_address);//课程地址
            address = (LinearLayout) view.findViewById(R.id.address);//点击地址
            listData = new ArrayList<>();
            for (int i = 1; i <= 2; i++) {
                listData.add("创意思维" + i);
            }

            imglist = new ArrayList<>();
            imglist.add("http://ww3.sinaimg.cn/large/610dc034jw1f5d36vpqyuj20zk0qo7fc.jpg");
            imglist.add("http://ww3.sinaimg.cn/large/610dc034jw1f5byokn81tj20dw0hiwfe.jpg");
            imglist.add("http://ww1.sinaimg.cn/large/7a8aed7bjw1f2sm0ns82hj20f00l8tb9.jpg");
            imglist.add("http://ww3.sinaimg.cn/large/7a8aed7bjw1f2x7vxkj0uj20d10mi42r.jpg");
            imglist.add("http://ww4.sinaimg.cn/large/7a8aed7bjw1eyaov0c9z4j20iz0sg40t.jpg");
            imglist.add("http://ww4.sinaimg.cn/large/7a8aed7bjw1exr0p4r0h3j20oy15445o.jpg");
            imglist.add("http://ww3.sinaimg.cn/large/610dc034jw1f5t889dhpoj20f00mi414.jpg");

            //机构课程
            mLvAdapter = new ClassAdpapter();
            listview.setAdapter(mLvAdapter);
            setListViewHeight(listview);
            //机构介绍
            Introduce_IMG_adapter adapter=new Introduce_IMG_adapter(getActivity(),imglist);
            listviewImg.setAdapter(adapter);
            setListViewHeight(listviewImg);

            address.setOnClickListener(listener);

        }
        return view;
    }
//    //机构介绍
//    public class ImgAdapter extends BaseAdapter{
//
//        @Override
//        public int getCount() {
//            return imglist==null?0:imglist.size();
//        }
//
//        @Override
//        public Object getItem(int i) {
//            return imglist.get(i);
//        }
//
//        @Override
//        public long getItemId(int i) {
//            return 0;
//        }
//
//        @Override
//        public View getView(int i, View view, ViewGroup viewGroup) {
//            CommonViewHolder holder = CommonViewHolder.createCVH(view, viewGroup, R.layout.item_institution_introduce);
//            ImageView imageView = holder.getIv(R.id.img);//图片
//
//            Picasso.with(getActivity())
//                    .load(imglist.get(i))
//                    .placeholder(R.drawable.ic_default_image)
////                    .transform(transformation)\
//                    .into(imageView);
//            return holder.convertView;
//        }
//
//    }
    //机构课程
    public class ClassAdpapter extends BaseAdapter {
        @Override
        public int getCount() {
            return listData == null ? 0 : listData.size();
        }

        @Override
        public Object getItem(int i) {
            return listData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            CommonViewHolder holder = CommonViewHolder.createCVH(view, viewGroup, R.layout.itme_resume);
            TextView title = holder.getTv(R.id.tv_title);//title
            TextView content = holder.getTv(R.id.tv_content);//课程时间等信息
            ImageView imageView = holder.getIv(R.id.iv);//图片

            ImageView iv = holder.getIv(R.id.iv);
            String s = listData.get(i);
            title.setText(s);
            return holder.convertView;
        }
    }
    //解决ScrollView包裹listview后只显示一个条目问题
    public void setListViewHeight(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
